package castledefense.game.render;

import castledefense.data.GameData;
import castledefense.data.Resources;
import castledefense.game.screens.MainGameScreen;
import castledefense.logic.Map;

import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class RenderSpellEffect {

	private ParticleEffect pyromancer;
	private boolean pyromancerDo = false;

	private ParticleEffect flood;
	private boolean floodDo = false;

	private ParticleEffect blessedWind;
	private boolean blessedWindDo = false;

	private int quakeDuration = 2000;
	private int quakeTimer = 0;

	public RenderSpellEffect() {
		final int halfScreenX = MainGameScreen.mapSizeX*Map.cellWidth/2;
		final int halfScreenY = MainGameScreen.mapSizeY*Map.cellWidth/2;
		pyromancer = Resources.pyromancerEffect;
		pyromancer.setPosition(halfScreenX, halfScreenY);

		flood = Resources.floodEffect;
		flood.setPosition(halfScreenX, halfScreenY);

		blessedWind = Resources.blessedWindEffect;
		blessedWind.setPosition(0f, GameData.SCREENSIZEY/2);
	}

	public void dispose() {
		pyromancer.dispose();
		flood.dispose();
		blessedWind.dispose();
	}

	/** After zooming and stuff */
	public void render(SpriteBatch batch, float delta) {
		if (pyromancerDo) {
			pyromancer.draw(batch, delta);
		}

		if (floodDo) {			
			flood.draw(batch, delta);
			
		}
		if (blessedWindDo) {
			batch.end();			
			batch.setProjectionMatrix(Cam.normalProjection);
			batch.begin();
			blessedWind.draw(batch, delta);
			batch.end();
			batch.setProjectionMatrix(Cam.getMatrix());
			batch.begin();
		}
		
		if (quakeTimer > 0) {
			final float amount = (float) quakeTimer / (float) quakeDuration;
			float shake = (float) Math.random() * 8f - 4f;
			float shatter = (float) Math.random() * 6f - 3f;
			shake *= amount;
			shatter *= amount;
			Cam.move(shake, shatter);
			Cam.zoom((float) Math.random()*0.01f -0.005f);
		}
	}

	public void update(int delta) {
		if (pyromancerDo) {
			pyromancerDo = !pyromancer.isComplete();
		}

		if (floodDo) {
			floodDo = !flood.isComplete();
		}

		if (blessedWindDo) {
			blessedWindDo = !blessedWind.isComplete();
		}

		quakeTimer = (quakeTimer > 0) ? (quakeTimer - delta) : 0;
	}

	public void addPyromancer() {
		pyromancer.start();
		pyromancerDo = true;
	}

	public void addFlood() {
		flood.start();
		floodDo = true;
	}

	public void addBlessedWind() {
		blessedWind.start();
		blessedWindDo = true;
	}

	public void addQuake() {
		quakeTimer = quakeDuration;
		// Cool sound effect here.
	}
	
}
